﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;
using University.Workshop.Presenters;
using University.Workshop.Presenters.PatientFolder;

namespace University.Workshop.WinApp
{
    public partial class SearchPatient : Form, ISearchPatient
    {
        int indexId;
        List<Patient> patient;
        PresenterSearchPatient presenter;

        public SearchPatient()
        {
            InitializeComponent();
            presenter = new PresenterSearchPatient(this);
        }

        private void SearchPatient_Load(object sender, EventArgs e)
        {
            indexId = -1;
            patient = (List<Patient>) presenter.SearchPatient();
            DataPatient.DataSource = patient;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            indexId = e.RowIndex;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            patient = (List<Patient>) presenter.SearchPatient();
            DataPatient.DataSource = patient;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (indexId != -1)
                {
                    
                    label2.Text = DataPatient.CurrentRow.Cells["PatientId"].Value.ToString();
                    PatientHistory patienthistory = new PatientHistory(label2.Text);
                    this.Visible = false;
                    if (patienthistory.ShowDialog() == DialogResult.Cancel)
                          this.Visible = true;
                          indexId = -1;
                          criterio.Clear();
                          DataPatient.DataSource = null; 
                }
                else
                {
                    MessageBox.Show("Seleccione un paciente");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void criterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        #region Miembros de ISearchPatient

        string ISearchPatient.FatherLastName
        {
            get
            {
                return criterio.Text;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
